package com.example.m2l_digicode;

import java.sql.DriverManager;
import java.sql.SQLException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;

public class EspaceDemandeur extends Activity {

	//Initialisation des variables globales
	String nom = null;
	String prenom = null;
	String adresse = null;
	String cp = null;
	String ville = null;
	String nb_frais = null;
	
	

    ResultSet result;	//Permet de recuperer le résultat d'une requete SQL
    PreparedStatement statement; // Permet d'initialiser la connexion au serveur
    Handler handler = null; 
    ProgressDialog loadingdialog;

    java.sql.Connection conn = null;
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_espace_demandeur);
		// Show the Up button in the action bar.
		//getActionBar().setDisplayHomeAsUpEnabled(true);
		
		//Récupération de l'adresse e-mail saisie précédement
	    Intent intent_demandeur = getIntent();
	    String mail = intent_demandeur.getStringExtra(Login.EXTRA_MESSAGE);
	     
		get_infos(mail);				
		
	}

	public void get_infos(final String mail)
	{
	    
		new Thread(new Runnable() {
	        @Override
	        public void run() {
	        	final String requete = "SELECT COUNT(ID_FRAIS), NOM_D, PRENOM_D, RUE, VILLE, CP FROM demandeur, ligne_frais " +
	        			"WHERE ligne_frais.ADRESSE_MAIL = demandeur.ADRESSE_MAIL" +
	        			" AND demandeur.ADRESSE_MAIL='"+mail+"'";
	        	
                runOnUiThread(new Runnable() {
					public void run() {

						loadingdialog = ProgressDialog.show(EspaceDemandeur.this,
                                "","Chargement en cours...",true);
					}
					});
	        	
	        	
			
		       try
		       {
		           
		           try {
		               Class.forName("com.mysql.jdbc.Driver").newInstance();
		        
		           } catch (InstantiationException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Instanciation Driver", e.getMessage());
		           } catch (IllegalAccessException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Access Exception", e.getMessage());
		           } catch (ClassNotFoundException e) {
		               // TODO Auto-generated catch block
		               e.printStackTrace();
		               Log.e("Class", e.getMessage());
		           }
		                    
		            
		           String url = "jdbc:mysql://192.168.1.26/fredi?autoReconnect=true";
		           //String url = "jdbc:mysql://ppe-fredi.suroot.com/fredi?autoReconnect=true";
		           String user = "util";
		           String passwd = "util123";
		            
		         
		           conn = (java.sql.Connection) DriverManager.getConnection(url, user, passwd);
		               Log.i("Connection", "Etablished");

		               statement=(PreparedStatement) conn.prepareStatement(requete);
		               result=(ResultSet) statement.executeQuery();
		               
		                while(result.next())
		                {
		                	nb_frais=result.getString(1);
		                	nom=result.getString(2);
		                	prenom=result.getString(3);
		                	adresse=result.getString(4);
		                	ville=result.getString(5);
		                	cp=result.getString(6);
		                }

		                
		                runOnUiThread(new Runnable() {
							public void run() {
								//R�cup�ration des Texview pour l'affichage
				                TextView tv_nom = (TextView) findViewById(R.id.tv_nom);
				                TextView tv_prenom = (TextView) findViewById(R.id.tv_prenom);
				                TextView tv_email = (TextView) findViewById(R.id.tv_email);
				                TextView tv_adresse = (TextView) findViewById(R.id.tv_adresse);
				                TextView tv_cp = (TextView) findViewById(R.id.tv_cp);
				                TextView tv_ville = (TextView) findViewById(R.id.tv_ville);
				                TextView tv_frais = (TextView) findViewById(R.id.tv_frais);
				                
				                //Affichage du resultat
				                tv_nom.setText(nom);
				                tv_prenom.setText(prenom);
				                tv_email.setText(mail);
				                tv_adresse.setText(adresse);
				                tv_cp.setText(cp);
				                tv_ville.setText(ville);
				                tv_frais.setText(nb_frais);
				                
		                        
				                loadingdialog.dismiss();
		                   }
		               });
	
		       }
		       
		       catch(SQLException e)
		       {
		           e.printStackTrace();
		                            
		           Log.e("MySQL", e.getMessage());
		         
		       }
			  
	        }
	          
	    }).start();
	    
	    
	}

}
